package com.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;
import com.utils.Utility;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class SendNewsToAllService implements Runnable{
    @Override
    public void run() {
        while(true){
            System.out.println("请输入要推送的消息：[输入exit退出]");
            String news= Utility.readString(100);
            if("exit".equals(news))break;
            Message message = new Message();
            message.setMesType(MessageType.MESSAGE_ALL_MES);
            message.setContent(news);
            message.setSender("服务器");
            message.setSendTime(new Date().toString());
            try {
                for (ServerConnectClientThread o :ManageServerConnectClientThread.getHm().values()) {
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(o.getSocket().getOutputStream());
                    objectOutputStream.writeObject(message);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
